import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userinfo: {},
    newinfo: {},
    logintime: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERINFO: (state, userinfo) => {
    state.userinfo = userinfo
  },
  SET_NEWINFO: (state, newinfo) => {
    state.newinfo = newinfo
    console.log('传入', state.newinfo)
  },
  Login_Time: (state, logintime) => {
    state.logintime = logintime
  }
}

const actions = {
  // user login
  login ({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        console.log('store-login-response', response)
        // const { data } = response
        // console.log('==', data)

        // commit('SET_TOKEN', data.token)
        // setToken(data.token)
        const data = response[0]
        // 这里使用用户的username作为token值
        commit('SET_TOKEN', data.username)
        setToken(data.username)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info获取用户信息
  getInfo ({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const data = response[0]

        if (!data) {
          console.log('data', data)
          return reject('Verification failed, please Login again.')
        }
        var date = new Date()
        // 年 getFullYear()：四位数字返回年份
        var year = date.getFullYear() // getFullYear()代替getYear()
        // 月 getMonth()：0 ~ 11
        var month = date.getMonth() + 1
        month = month < 10 ? ('0' + month) : month
        // 日 getDate()：(1 ~ 31)
        var day = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
        // 时 getHours()：(0 ~ 23)
        var hour = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
        // 分 getMinutes()： (0 ~ 59)
        var minute = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
        // 秒 getSeconds()：(0 ~ 59)
        var second = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()

        var time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
        commit('SET_NAME', data.name)
        commit('SET_AVATAR', data.avatar)
        commit('SET_USERINFO', data)
        commit('Login_Time', time)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 设置新闻
  getnewInfo ({ commit, state }, data) {
    console.log('执行了', data)
    commit('SET_NEWINFO', data)
  },
  // 修改信息后更新userinfo
  updateuserInfo ({ commit, state }, data) {
    console.log('更新userinfo', data)
    commit('SET_USERINFO', data)
  },
  // user logout退出登录
  logout ({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken ({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

